package org.linlinjava.litemall.admin.service;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.domain.LumiereCoupon;
import org.linlinjava.litemall.db.domain.LumiereCouponStrategy;
import org.linlinjava.litemall.db.service.LumiereCouponService;
import org.linlinjava.litemall.db.service.LumiereCouponStrategyService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Random;

@Service
@RequiredArgsConstructor
public class AdminLumiereCouponService {

    private final LumiereCouponService lumiereCouponService;

    public final LumiereCouponStrategyService lumiereCouponStrategyService;

    public ResponseUtil.Response coupons(Integer limit, Integer page, Integer status, String name) {
        IPage<LumiereCoupon> lumiereCoupons = lumiereCouponService.page(new Page<>(page,limit),Wrappers.lambdaQuery(LumiereCoupon.class)
                .in(status != null, LumiereCoupon::getStatus, status)
                .like(StrUtil.isNotEmpty(name), LumiereCoupon::getName, name));
        return ResponseUtil.okPage(lumiereCoupons);
    }


    public ResponseUtil.Response addConpon(LumiereCoupon coupon) {
        //创建优惠券码
        String code = RandomUtil.randomNumbers(8);
        LumiereCoupon exist = lumiereCouponService.getOne(Wrappers.lambdaQuery(LumiereCoupon.class)
                .eq(LumiereCoupon::getName, coupon.getName()));
        if (ObjectUtil.isNotNull(exist)){
            return ResponseUtil.success(100);
        }
        if(lumiereCouponService.getOne(Wrappers.lambdaQuery(LumiereCoupon.class)
                .eq(LumiereCoupon::getName, coupon.getName()))!=null){
            return addConpon(coupon);
        }
        coupon.setCode(Integer.valueOf(code));
        coupon.setAddTime(LocalDateTime.now());
        coupon.setUpdateTime(LocalDateTime.now());
        coupon.setDeleted(false);
        return lumiereCouponService.save(coupon)?ResponseUtil.ok():ResponseUtil.fail();
    }

    public ResponseUtil.Response findConpon(Long id) {
        return ResponseUtil.ok(lumiereCouponService.getById(id));
    }

    public ResponseUtil.Response removeConpon(Long id) {
        List<LumiereCouponStrategy> list = lumiereCouponStrategyService.list(Wrappers.lambdaQuery(LumiereCouponStrategy.class).eq(LumiereCouponStrategy::getCouponId, id));
        if(!list.isEmpty()){
            return ResponseUtil.fail(401,"策略正在使用");
        }
        return ResponseUtil.ok(lumiereCouponService.removeById(id));
    }

    @Transactional
    public ResponseUtil.Response editConpon(LumiereCoupon coupon) {
        return lumiereCouponService.updateById(coupon)?ResponseUtil.ok():ResponseUtil.fail();
    }

    public ResponseUtil.Response statusCoupon(LumiereCoupon coupon) {
        if(coupon.getStatus().equals(1)){
            List<LumiereCouponStrategy> lumiereCouponStrategies = lumiereCouponStrategyService.list(Wrappers.lambdaQuery(LumiereCouponStrategy.class)
                    .eq(LumiereCouponStrategy::getCouponId, coupon.getId())
                    .eq(LumiereCouponStrategy::getStatus,1));
            if(!lumiereCouponStrategies.isEmpty()){
                return ResponseUtil.fail(202,"该券已存在开启的策略之中");
            }
        }
        coupon.setStatus(coupon.getStatus().equals(0)?1:0);
        return lumiereCouponService.updateById(coupon)?ResponseUtil.ok():ResponseUtil.fail();
    }

    public ResponseUtil.Response couponOption() {
        List<LumiereCoupon> lumiereCoupons = lumiereCouponService.list(Wrappers.lambdaQuery(LumiereCoupon.class)
                .eq(LumiereCoupon::getDeleted, 0));
        //获取已使用券
        List<Long> cplongs = lumiereCouponService.usedCoupons();
        for (LumiereCoupon lumiereCoupon : lumiereCoupons) {
            for (Long cplong : cplongs) {
                if(lumiereCoupon.getId().equals(cplong)){
                    lumiereCoupon.setStatus(2);
                }
            }
        }
        return ResponseUtil.ok(lumiereCoupons);
    }
}
